package service;

import common.Message;
import entity.Chat;
import entity.User;
import tools.ChatViewManage;
import tools.ThreadManage;
import view.ChatView;

import java.util.ArrayList;

public class ChatService {

    /**
     * 发送消息
     *
     * @param message
     */
    public void send(Message message) {
        Chat content = (Chat) message.getContent();
        ClientThread thread = ThreadManage.getThread(content.getSenderQQ());
        thread.send(message);
    }

    /**
     * 接收消息
     *
     * @param msg
     */
    public void receive(Message msg) {
        Chat content = (Chat) msg.getContent();
        // 检查聊天窗口是否已经打开
        ChatView chatFrame = ChatViewManage.getChatFrame(content.getSenderQQ());
        if (chatFrame != null) {// 聊天窗口已经打开
            chatFrame.receiveChat(content);
        } else {// 聊天窗口未打开
            ArrayList<User> users = (ArrayList<User>) msg.getRemark();
            User u = users.get(1);
            User f = users.get(0);
            ChatView chatView = new ChatView(u, f);
            ChatViewManage.addChatFrame(f.getQq(), chatView);
        }
    }

}
